package com.xafc.demo.security.handle;

import com.xafc.demo.framework.utils.RequestUtils;
import com.xafc.demo.security.utils.ApiResultUtil;
import com.xafc.demo.framework.result.ApiResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        if (RequestUtils.isAjax(request)){
            ApiResultUtil.responseJson(response, ApiResult.deny("请先登录"));
        }else {
            response.sendRedirect(request.getContextPath() + "/login");
        }
    }
}